package com.example.administrator.rfidreadcardtets;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    RequestQueue requestQueue;
private EditText et1,et2;
    private Button bt1,bt2;
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        requestQueue=MyApplication.getsRequestQueue();

    }
    private void init(){
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        bt1=(Button) findViewById(R.id.button);
        bt2=(Button) findViewById(R.id.button2);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);



    }


    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.button:
                String url = "http://172.22.232.102:8080/andrioe/LoginAction?username="+et1.getText()+
                        "&pass="+et2.getText();
                StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        if (s.equals("success")) {
                            Log.d("login=======", "success");
                            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.d("Login======","Error======");
                            }
                        }
                );
                requestQueue.add(stringRequest);
                break;
            case R.id.button2:
        }
    }
}
